package com.mijie.homi.service.album;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.exception.RecordNotFoundException;
import com.mijie.homi.dao.album.AlbumDao;
import com.mijie.homi.dao.album.PhotoDao;
import com.mijie.homi.domain.album.Album;
import com.mijie.homi.domain.album.Photo;
import com.mijie.homi.domain.commom.Review;
import com.mijie.homi.domain.commom.Review.Module;
import com.mijie.homi.listener.IntegrationHandler;
import com.mijie.homi.service.common.AbstractEventService;
import com.mijie.homi.service.user.UserExtInfoService;

@Service
public class AlbumService extends AbstractEventService {

	@Autowired
	private AlbumDao albumDao;
	@Autowired
	private PhotoDao photoDao;
	@Autowired
	private IntegrationHandler integrationHandler;
	@Autowired
	private UserExtInfoService userExtInfoService;
	
	@Override
	public Module getModule() {
		return Review.Module.PHOTO;
	}
	
	public int deleteAlbum(int id,int userId){
		return albumDao.deleteAlbum(id,userId);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int createAlbum(Album album){
		album.setIdx(Long.MAX_VALUE-System.currentTimeMillis());
		return albumDao.createAlbum(album);
	}
	
	public Album getAlbum(int id){
		return albumDao.getAlbum(id);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int uploadPhoto(int userId,String albumDescribes,List<Photo> list){
		Album album = new Album();
		album.setUserId(userId);
		album.setDescribes(albumDescribes);
		album.setCreateAt(new Date());
		int albumId = createAlbum(album);
		if(albumId>0){
			for(Photo p : list)
				p.setAlbumId(albumId);
		}
		int rows =  photoDao.uploadPhoto(list);
		if(rows>0){
			userExtInfoService.incrNumOfPhoto(userId, list.size());
			integrationHandler.uploadPhoto(userId);
		}
		return rows;
	}
	
	public List<Photo> listLatestPhoto(int userId,int size){
		return photoDao.listLatestPhoto(userId, size);
	}
	
	public int deletePhoto(int userId,long id){
		int rows =  photoDao.deletePhoto(userId, id);
		if(rows>0) userExtInfoService.decrNumOfPhoto(userId);
		return rows;
	}
	
	public int likePhoto(long id){
		return photoDao.likePhoto(id);
	}
	
	public Photo getPhoto(long id){
		return photoDao.getPhoto(id);
	}
	public List<Album> listAlbum(int userId,long idx,int limit){
		return albumDao.listAlbum(userId, idx, limit);
	}
	
	public List<Photo> listPhotoByAlbumId(int albumId){
		return photoDao.listPhotoByAlbumId(albumId);
	}
	
	public List<Photo> listPhotoByUserId(int userId,Long idx,Integer limit){
		return photoDao.listPhotoByUserId(userId, idx, limit);
	}

	
	@Override
	public int getOwner(long id) throws RecordNotFoundException{
		Photo photo = this.getPhoto(id);
		if(photo == null) throw new RecordNotFoundException(id);
		return photo.getUserId();
	}

	@Override
	@Transactional(rollbackFor={Exception.class})
	public int increNumOfReview(long id) {
		return photoDao.increNumOfReview(id);
	}

	@Override
	@Transactional(rollbackFor={Exception.class})
	protected int decreNumOfReview(long id) {
		return photoDao.decreNumOfReview(id);
	}

}
